//
//  OrgDetailController.m
//  easystudy
//
//  Created by kane on 15/6/11.
//  Copyright (c) 2015年 kane. All rights reserved.
//

#import "OrgDetailController.h"
#import "CourseDetailController.h"
#import "CourseCell.h"

@interface OrgDetailController ()<UITableViewDataSource,UITableViewDelegate>
{
    __weak IBOutlet UIView *_orgInfoView;
    __weak IBOutlet UILabel *titleLbl;
    __weak IBOutlet UILabel *addressLbl;
    __weak IBOutlet UILabel *telephoneLbl;
    
    __weak IBOutlet UITableView *courseTable;
    
    NSMutableArray *dataList;
}

@end

@implementation OrgDetailController
@synthesize orgDict;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dataList = [[NSMutableArray alloc] init];
    
    courseTable.tableHeaderView = _orgInfoView;
    
    NSLog(@"orgDict:%@",orgDict);
    titleLbl.text = orgDict[@"name"];
    addressLbl.text = orgDict[@"address"];
    telephoneLbl.text = orgDict[@"telephone"];
    [self getCourseData];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - User Actions

- (IBAction)backBtnPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getCourseData
{
    NSDictionary *params = @{@"organization":orgDict[@"id"]};
    [WebService findCourseList:params WithBlock:^(id result, NSError *error) {
        if (!error) {
            if ([result[@"result"] intValue] == 1) {
                [dataList addObjectsFromArray:result[@"course"]];
                [courseTable reloadData];
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
}

#pragma mark - UITableView DataSource && Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"CourseCell";
    CourseCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CourseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *item = dataList[indexPath.row];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",BaseURL,item[@"logo_url"]];
    NSString *startDate = [item[@"start_date"] substringToIndex:10];
    NSString *endDate = [item[@"end_date"] substringToIndex:10];
    
    [cell.logoImgView setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:nil];
    cell.subjectLabel.text = item[@"subject"];
    cell.scopeLabel.frame = CGRectMake(92, 28, 220, 15);
    cell.scopeLabel.text = item[@"desc"];
    cell.scopeLabel.numberOfLines = 0;
    [cell.scopeLabel sizeToFit];
    if (cell.scopeLabel.frame.size.height / 13.f > 3) {
        cell.scopeLabel.numberOfLines = 3;
        [cell.scopeLabel sizeToFit];
    }
    cell.dateLabel.frame = CGRectMake(92, cell.scopeLabel.frame.origin.y + cell.scopeLabel.frame.size.height + 2, 220, 15);
    cell.dateLabel.text = [NSString stringWithFormat:@"活动时间：%@至%@",startDate,endDate];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


@end
